﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    public int MaDe = 0;
    public int code = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["user"] == null)
            Response.Redirect("dangnhap.aspx");
        if (Session["BaiThi"] == null)
            Response.Redirect("chonde.aspx");
        if (!IsPostBack) {
            int MaBaiThi = int.Parse(Session["BaiThi"].ToString());
            BaiThiINFO mBaiThi = BaiThiDAL.getInfo(MaBaiThi);
            MaDe = mBaiThi.MaDe;
            code = MaBaiThi;
            loadDeThi(mBaiThi);
            Page.Title = "Website thi trắc nghiệm";

            HttpCookie IDCookie = new HttpCookie("ID");
            IDCookie.Value = Session.SessionID;
            IDCookie.HttpOnly = true;
            HttpCookie NameCookie = new HttpCookie("Name");
            NameCookie.Value = Session["HoTen"].ToString();
            NameCookie.HttpOnly = true;
            HttpCookie BDCookie = new HttpCookie("bidanh");
            BDCookie.Value = Session["user"].ToString();
            BDCookie.HttpOnly = true;
            Response.Cookies.Add(IDCookie);
            Response.Cookies.Add(NameCookie);
            Response.Cookies.Add(BDCookie);
        }
    }

    private void loadDeThi(BaiThiINFO mBaiThi)
    {
        DeThiINFO mDethi = DeThiDAL.GetInfo(mBaiThi.MaDe);
        lblDeThi.Text = mDethi.TenDe;
        string sdate = mDethi.ThoiGianThi.ToString();
        mBaiThi.TinhTrang = 1;
        if (mBaiThi.ThoiGianBatDau.Equals(mBaiThi.NgayThi))
        {
            mBaiThi.ThoiGianBatDau = DateTime.Now;
            mBaiThi.ThoiGianKetThuc = DateTime.Now;
            mBaiThi.ThoiGianThi = mDethi.ThoiGianThi * 60;
            sdate = mBaiThi.ThoiGianThi.ToString();
        }
        else {
           // TimeSpan t = TimeSpan.FromSeconds(mBaiThi.ThoiGianThi);
            sdate = mBaiThi.ThoiGianThi.ToString();
            mBaiThi.ThoiGianKetThuc = DateTime.Now;
        }
        BaiThiDAL.Update(mBaiThi);
        timeExpired.Value = sdate;
    }
}